{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 197,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:23.304981900Z",
     "start_time": "2023-11-22T12:03:23.294894700Z"
    }
   },
   "outputs": [],
   "source": [
    "import json\n",
    "from datetime import datetime, timedelta\n",
    "from time import sleep\n",
    "import pandas as pd\n",
    "import re\n",
    "from fontTools.ttLib import TTFont\n",
    "import requests\n",
    "\n",
    "headers = {\n",
    "    'Accept': 'application/json, text/plain, */*',\n",
    "    'Accept-Encoding': 'gzip, deflate, br',\n",
    "    'Accept-Language': 'zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6',\n",
    "    'Connection': 'keep-alive',\n",
    "    'Cookie': '_lxsdk_cuid=18bb8e54a52c8-05d74dafedd255-4c657b58-1fa400-18bb8e54a52c8; WEBDFPID=575uyu2zuv49544xz7y888wu6w1y3z9681yy1v31uwz979583021x4y2-2014974199294-1699614198574MQISOUIfd79fef3d01d5e9aadc18ccd4d0c95072811; token=AgHgH4JeCsL56Wf0sTuGdNG58jvtzYIOfuviDdrFBgyDcdDJTIzTi7sSEjEctiHF0QHRcy5oEU6X3AAAAAAaHAAAEQ13krTAkT10BgOiQ4Au66X2tOgGX9KxfIgLFdRmsoksklfSKQrJHUly90LD7qmb; uuid=18bb8e54a52c8-05d74dafedd255-4c657b58-1fa400-18bb8e54a52c8; Hm_lvt_703e94591e87be68cc8da0da7cbd0be2=1699614237,1700572070; Hm_lpvt_703e94591e87be68cc8da0da7cbd0be2=1700572312; _lxsdk=18bb8e54a52c8-05d74dafedd255-4c657b58-1fa400-18bb8e54a52c8; _lx_utm=utm_source%3DBaidu%26utm_medium%3Dorganic; _lxsdk_s=18bf1f19731-200-57b-4e5%7C%7C101',\n",
    "    'Host': 'piaofang.maoyan.com',\n",
    "    'Referer': 'https://piaofang.maoyan.com/dashboard/movie?date=2023-11-01&movieId=1466204',\n",
    "    'Sec-Ch-Ua': '\"Microsoft Edge\";v=\"119\", \"Chromium\";v=\"119\", \"Not?A_Brand\";v=\"24\"',\n",
    "    'Sec-Ch-Ua-Mobile': '?0',\n",
    "    'Sec-Ch-Ua-Platform': \"Windows\",\n",
    "    'Sec-Fetch-Dest': 'empty',\n",
    "    'Sec-Fetch-Mode': 'cors',\n",
    "    'Sec-Fetch-Site': 'same-origin',\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',\n",
    "    'X-For-With': '4s47ITaYu+yQrNmzTgtmRUqG4nVyamtVLs3dxw3W9Vr4vfeW7rpXVAQY9CQh5nsufc6WxwRrBmRzN01Fv4X9eO8QVWp5KMNkVOjxP6ckVZtzH2PCc53PP6bQzWK7YtemEaY/89UYdD4NsSg7LiBtEg/FSldlab1tl2zCMtQFZxb26Uqx6ylGsK5iyKNYCMWOh+i8CWNrmobH7XkT/eM74w=='\n",
    "}\n",
    "\n",
    "url = 'https://piaofang.maoyan.com/dashboard-ajax/movie'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 198,
   "outputs": [],
   "source": [
    "params = {\n",
    "    'showDate': '20231121',\n",
    "    'movieId': '1466204',\n",
    "    'orderType': '0',\n",
    "    'uuid': '18bb8e54a52c8-05d74dafedd255-4c657b58-1fa400-18bb8e54a52c8',\n",
    "    'timeStamp': '1700572426765',\n",
    "    'User-Agent': 'TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMDsgV2luNjQ7IHg2NCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzExOS4wLjAuMCBTYWZhcmkvNTM3LjM2IEVkZy8xMTkuMC4wLjA=',\n",
    "    'index': '882',\n",
    "    'channelId': '40009',\n",
    "    'sVersion': '2',\n",
    "    'signKey': '1129721d872502a4374d069c85ffb292'\n",
    "}\n",
    "response = requests.get(url, headers=headers, params=params)\n",
    "json_data = json.loads(response.content.decode())\n",
    "movie_list = json_data.get('movieList', {}).get('list', [])\n",
    "font_style = json_data.get('fontStyle', '')\n",
    "woff_url = re.findall('url\\(\"(.*?\\.woff)\"\\)', font_style)[0].split(',')[-1]\n",
    "woff_url = 'https:' + woff_url[5:]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:23.970777500Z",
     "start_time": "2023-11-22T12:03:23.302015900Z"
    }
   },
   "id": "896c719778c99e18"
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "outputs": [],
   "source": [
    "woff_headers = {\n",
    "    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0',\n",
    "}\n",
    "\n",
    "woff_resp = requests.get(woff_url, headers=woff_headers)\n",
    "with open(\"../static/font/mao_yan.woff\", \"wb\") as code:\n",
    "    code.write(woff_resp.content)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:24.237763700Z",
     "start_time": "2023-11-22T12:03:23.972773200Z"
    }
   },
   "id": "ac6af9fdc47699cd"
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2 extra bytes in post.stringData array\n"
     ]
    }
   ],
   "source": [
    "base_font = TTFont('../static/base.woff')\n",
    "base_num_list = ['.', '7', '3', '6', '1', '2', '8', '0', '4', '9', '5']\n",
    "base_unicode_list = [\n",
    "    'x', 'uniEB92', 'uniE8D7', 'uniF7FF', 'uniF85E', 'uniE99C',\n",
    "    'uniF1FC', 'uniF726', 'uniE8EE', 'uniE9EA', 'uniECDC'\n",
    "]\n",
    "mao_yan_font = TTFont('../static/font/mao_yan.woff')\n",
    "mao_yan_unicode_list = mao_yan_font['cmap'].tables[0].ttFont.getGlyphOrder()\n",
    "mao_yan_num_list = []\n",
    "mao_yan_new_unicode_list = []\n",
    "mao_yan_num_map = {}\n",
    "\n",
    "# for i in range(1, 12):\n",
    "#     mao_yan_glyph = mao_yan_font['glyf'][mao_yan_unicode_list[i]]\n",
    "#     for j in range(11):\n",
    "#         base_glyph = base_font['glyf'][base_unicode_list[j]]\n",
    "#         if mao_yan_glyph.flags == base_glyph.flags:\n",
    "#             mao_yan_num_list.append(base_num_list[j])\n",
    "#             mao_yan_new_unicode_list.append(mao_yan_unicode_list[i])\n",
    "#             break\n",
    "# for i in zip(mao_yan_num_list, mao_yan_new_unicode_list):\n",
    "#     mao_yan_num_map[i[1]] = i[0]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:24.249812400Z",
     "start_time": "2023-11-22T12:03:24.240755600Z"
    }
   },
   "id": "869e76dd62b5c622"
  },
  {
   "cell_type": "code",
   "execution_count": 201,
   "outputs": [],
   "source": [
    "# code = movie_list[0].get('boxSplitUnit', {}).get('num', '')\n",
    "# code_list = code.split('.')\n",
    "# num = []\n",
    "# for i in code_list:\n",
    "#     num_code_list = re.findall('&#x(\\w{4});', i)\n",
    "#     num_list = []\n",
    "#     for j in num_code_list:\n",
    "#         num_code = 'uni' + j.upper()\n",
    "#         num_list.append(mao_yan_num_map[num_code])\n",
    "#     num.append(''.join(num_list))\n",
    "# '.'.join(num)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:24.283762Z",
     "start_time": "2023-11-22T12:03:24.247817Z"
    }
   },
   "id": "22f8f5018538dd4"
  },
  {
   "cell_type": "code",
   "execution_count": 202,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2 extra bytes in post.stringData array\n"
     ]
    }
   ],
   "source": [
    "\n",
    "from reportlab.graphics import renderPM\n",
    "from fontTools.pens.reportLabPen import ReportLabPen\n",
    "from reportlab.graphics.shapes import Path, Group, Drawing\n",
    "from reportlab.lib import colors\n",
    "from PIL import Image\n",
    "import cv2\n",
    "\n",
    "base = TTFont('../static/base.woff')\n",
    "mao_yan_font = TTFont('../static/font/mao_yan.woff')\n",
    "gs = mao_yan_font.getGlyphSet()\n",
    "for i in mao_yan_font.getGlyphNames():\n",
    "    g = gs[i]\n",
    "    pen = ReportLabPen(gs, Path(fillColor=colors.red, strokeWidth=7))\n",
    "    g.draw(pen)\n",
    "    w, h = 2100, 2100\n",
    "    g = Group(pen.path)\n",
    "    g.translate(300, 300)\n",
    "    d = Drawing(w, h)\n",
    "    d.add(g)\n",
    "    image_file = '../static/img' + \"/\" + i + \".png\"\n",
    "    renderPM.drawToFile(d, image_file, 'png')\n",
    "    img = cv2.imread(image_file, -1)\n",
    "    height, width = img.shape[:2]\n",
    "    # 缩小图像\n",
    "    size = (int(w * 0.05), int(h * 0.05))\n",
    "    img = cv2.resize(img, size, cv2.INTER_AREA)\n",
    "    cv2.imwrite(image_file, img)\n",
    "    image_buffer = Image.open(image_file)\n",
    "    mark_img = Image.new('RGBA', size, (255, 0, 0, 0))\n",
    "    mark_img.paste(image_buffer, (0, 0))\n",
    "    mark_img.save(image_file)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:49.491323900Z",
     "start_time": "2023-11-22T12:03:24.282764100Z"
    }
   },
   "id": "22a2cf2008e95dda"
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "outputs": [
    {
     "data": {
      "text/plain": "['uniE3DF',\n 'uniE3EC',\n 'uniEA60',\n 'uniEA6F',\n 'uniEB19',\n 'uniED30',\n 'uniEF28',\n 'uniF11C',\n 'uniF3E8',\n 'uniF7D2']"
     },
     "execution_count": 203,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mao_yan_font.getGlyphNames()[1:-1]"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:49.498140700Z",
     "start_time": "2023-11-22T12:03:49.490325200Z"
    }
   },
   "id": "3784ea2b77f7731f"
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "outputs": [
    {
     "data": {
      "text/plain": "{'uniE3DF': '7',\n 'uniE3EC': '2',\n 'uniEA60': '6',\n 'uniEA6F': '0',\n 'uniEB19': '9',\n 'uniED30': '1',\n 'uniEF28': '8',\n 'uniF11C': '3',\n 'uniF3E8': '5',\n 'uniF7D2': '4'}"
     },
     "execution_count": 204,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from PIL import Image\n",
    "import pytesseract\n",
    "\n",
    "pytesseract.pytesseract.tesseract_cmd = r'D:\\software\\Tesseract-OCR\\tesseract.exe'\n",
    "\n",
    "img_list = mao_yan_font.getGlyphNames()[1:-1]\n",
    "num_map = {}\n",
    "for i in img_list:\n",
    "    image = Image.open('../static/img/%s.png' % i)\n",
    "    image = image.convert('L')\n",
    "    text = pytesseract.image_to_string(image, lang='chi_sim',\n",
    "                                       config=r'-c tessedit_char_whitelist=0123456789 --psm 6')\n",
    "    num_map[i] = text.replace('\\n', '')[0]\n",
    "num_map"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:51.546568200Z",
     "start_time": "2023-11-22T12:03:49.499168Z"
    }
   },
   "id": "991b7b6bb1430c33"
  },
  {
   "cell_type": "code",
   "execution_count": 205,
   "outputs": [
    {
     "data": {
      "text/plain": "'904.27'"
     },
     "execution_count": 205,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "code = movie_list[0].get('boxSplitUnit', {}).get('num', '')\n",
    "code_list = code.split('.')\n",
    "num = []\n",
    "for i in code_list:\n",
    "    num_code_list = re.findall('&#x(\\w{4});', i)\n",
    "    num_list = []\n",
    "    for j in num_code_list:\n",
    "        num_code = 'uni' + j.upper()\n",
    "        num_list.append(num_map[num_code])\n",
    "    num.append(''.join(num_list))\n",
    "'.'.join(num)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2023-11-22T12:03:51.553074500Z",
     "start_time": "2023-11-22T12:03:51.541314Z"
    }
   },
   "id": "3048a9dd2884ac16"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
